package com.mayjean.core.config;

import com.mayjean.common.NezhaConstant;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.concurrent.Executor;


/**
 * Nezha async executor configuration
 *
 * @author kyleju
 */
@Configuration
@EnableAsync
@EnableScheduling
@AutoConfigureAfter(NezhaConfiguration.class)
@Import(NezhaConfiguration.class)
@Lazy
public class NezhaAsyncExecutorConfiguration extends AsyncConfigurerSupport {

	private final TaskExecutor executor;

	public NezhaAsyncExecutorConfiguration(@Qualifier(NezhaConstant.DEFAULT_TASK_EXECUTOR) TaskExecutor executor) {
		this.executor = executor;
	}

	@Override
	public Executor getAsyncExecutor() {
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return new SimpleAsyncUncaughtExceptionHandler();
	}
}
